package com.maimao.info.controller;

import com.maimao.api.mall.info.CollectionApi;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.info.controller.base.BaseController;
import com.maimao.info.service.CollectionService;
import com.maimao.model.info.Collect;
import com.maimao.model.info.vo.FollowGoods;
import com.maimao.model.user.Foot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2021/1/11 5:23 下午
 */
@RestController
public class CollectionController extends BaseController implements CollectionApi {

    @Autowired
    private CollectionService collectionService;

    /**
     * 添加收藏 宝贝/店铺
     */
    @Override
    @PostMapping("/add")
    public Result<?> addCollect(@RequestBody List<Collect> collectList) {
        if (collectList == null || collectList.size() < 0) {
            return Result.notValid("请选择要收藏的宝贝或店铺");
        }
        AuthUser authUser = checkAndGetAuthUser();
        Long useId = Long.valueOf(authUser.getId());
        for (Collect collect : collectList) {
            if (collect.getTargetId() == null || collect.getTargetId() < 0 || collect.getType() == null ||
                    (!collect.getType().equals(1) && !collect.getType().equals(0))) {
                return Result.notValid();
            } else {
                collect.setUserId(useId);
                collect.setCreateTime(DateUtils.now());
                collect.setDeleted(0);
            }
        }
        collectionService.addCollectionBatch(collectList);
        return Result.ok("收藏成功");
    }

    /**
     * 关注的商品
     */
    @Override
    @GetMapping("/follow-goods")
    public Result<?> collectGoodsList() {
        AuthUser authUser = checkAndGetAuthUser();
        Long userId = authUser.getId2Long();
        List<FollowGoods> followGoodsList = collectionService.followGoodsList(userId);
        return Result.okData(followGoodsList);
    }

    @Override
    @DeleteMapping("/remove")
    public Result<?> remove(@RequestBody Map<String, Long> data) {
        if (data==null||data.get("id")==null){
            return Result.notValid();
        }
        Long id = data.get("id");
        if (id<=0){
            return Result.notValid();
        }
        collectionService.remove(id);
        return Result.ok();
    }


}
